
#include "testArray.h"

CPPUNIT_TEST_SUITE_REGISTRATION (testArray);

void testArray :: setUp (void){
}

void testArray :: tearDown (void) {
}

void testArray :: testMakeInstance (void)
{
	array test;
}

void testArray :: testSetEntry(void)
{
	array test;
	test["key"] = "value";
}

void testArray :: testSetAndGetEntry(void)
{
	array test;
	test["key"] = "value";
	CPPUNIT_ASSERT_EQUAL ((string)"value", test["key"].get());
}

void testArray :: testSetAndGetEntryInArray(void)
{
	array test;
	test["key"]["key2"] = "value";
	CPPUNIT_ASSERT_EQUAL ((string)"value", test["key"]["key2"].get());
}

void testArray :: testSetAndGetEntryInBigArray(void)
{
	array test;
	test["key"]["key2"]["key3"]["key4"]["key5"]["key6"]["key7"]["key8"] = "value";
	CPPUNIT_ASSERT_EQUAL ((string)"", test["key"].get());
	CPPUNIT_ASSERT_EQUAL ((string)"value", test["key"]["key2"]["key3"]["key4"]["key5"]["key6"]["key7"]["key8"].get());

}

void testArray :: testSetAndGetEntryInManyArrays(void)
{
	array test;
	test["key1"] = "value";
	test["key"]["key2"] = "value1";
	test["key2"]["key"] = "value2";
	test["key"]["key3"] = "value3";
	test["a"]["b"] = "c";
	CPPUNIT_ASSERT_EQUAL ((string)"value", test["key1"].get());
	CPPUNIT_ASSERT_EQUAL ((string)"value1", test["key"]["key2"].get());
	CPPUNIT_ASSERT_EQUAL ((string)"value2", test["key2"]["key"].get());
	CPPUNIT_ASSERT_EQUAL ((string)"value3", test["key"]["key3"].get());
	CPPUNIT_ASSERT_EQUAL ((string)"c", test["a"]["b"].get());
}


void testArray :: testToStringWithOneEntry(void) {
	array test;
	test["key"] = "value";
	CPPUNIT_ASSERT_EQUAL ((string)"a:1:{s:3:\"key\";s:5:\"value\";}", test.toString());
}


void testArray :: testToStringWithTwoEntry(void) {
	array test;
	test["key1"] = "value1";
	test["key2"] = "value2";
	CPPUNIT_ASSERT_EQUAL ((string)"a:2:{s:4:\"key1\";s:6:\"value1\";s:4:\"key2\";s:6:\"value2\";}", test.toString());
}

void testArray :: testToStringWithManyArrays(void) {
	array test;
	test["key1"]["key2"]["key3"]["key4"]["key5"]["key6"]["key7"] = "value1";
	CPPUNIT_ASSERT_EQUAL ((string)"a:1:{s:4:\"key1\";a:1:{s:4:\"key2\";a:1:{s:4:\"key3\";a:1:{s:4:\"key4\";a:1:{s:4:\"key5\";a:1:{s:4:\"key6\";a:1:{s:4:\"key7\";s:6:\"value1\";}}}}}}}",test.toString());
}

void testArray :: testFromStringWithOneEntry(void) {
	array test;
	test.fromString("a:1:{s:3:\"key\";s:5:\"value\";}");
	CPPUNIT_ASSERT_EQUAL ((string)"value", test["key"].get());
}

void testArray :: testFromStringWithTwoEntry(void) {
	array test;
	test.fromString("a:2:{s:4:\"key1\";s:6:\"value1\";s:4:\"key2\";s:6:\"value2\";}");
	CPPUNIT_ASSERT_EQUAL ((string)"value1", test["key1"].get());
	CPPUNIT_ASSERT_EQUAL ((string)"value2", test["key2"].get());
}

void testArray :: testFromStringWithManyArrays(void) {
	array test;
	test.fromString("a:1:{s:4:\"key1\";a:1:{s:4:\"key2\";a:1:{s:4:\"key3\";a:1:{s:4:\"key4\";a:1:{s:4:\"key5\";a:1:{s:4:\"key6\";a:1:{s:4:\"key7\";s:6:\"value1\";}}}}}}}");
	CPPUNIT_ASSERT_EQUAL ((string)"value1", test["key1"]["key2"]["key3"]["key4"]["key5"]["key6"]["key7"].get());
}

void testArray :: testIsEmptyWithEmptyArrayReturnsTrue(void) {
	array test;
	CPPUNIT_ASSERT_EQUAL (true, test.isEmpty());

}

void testArray :: testIsEmptyWithNotEmptyArrayReturnsFalse(void) {
	array test;
	test["test"] = "value";
	CPPUNIT_ASSERT_EQUAL (false, test.isEmpty());

}

void testArray :: testKeyFromArrayReturnsTestkey(void) {
	array test;
	test["Testkey"] = "value";
	CPPUNIT_ASSERT_EQUAL ((string)"Testkey", test.key());

}

void testArray :: testCopyArrayWithNewData(void) {
	array test;
	test["Testkey"] = "value";
	test["Copykey"] = "copyok";

	CPPUNIT_ASSERT_EQUAL ((string)"value", test["Testkey"].get());

	array test2;
	test2["Testkey"] = "value1";
	test2["Testkey2"] = "value2";
//	CPPUNIT_ASSERT_EQUAL ((string)"value2", test2["Testkey"].get());

	test2 = test;
	CPPUNIT_ASSERT_EQUAL ((string)"value2", test2["Testkey2"].get());
	CPPUNIT_ASSERT_EQUAL ((string)"value", test2["Testkey"].get());
	CPPUNIT_ASSERT_EQUAL ((string)"copyok", test2["Copykey"].get());
}
